import { useAppStore } from '~/stores/app'

function hasPermission(value, el = false) {
    const appStore = useAppStore()

    if (!Array.isArray(value)) {
        throw new Error(`需要配置权限，例如 v-permission="['getStatistics3,GET']"`)
    }
    const hasAuth = value.findIndex((v) => appStore.ruleNames.includes(v)) != -1
    if (el && !hasAuth) {
        el.parentNode && el.parentNode.removeChild(el)
    }
    return hasAuth
}

const permission = {
    mounted(el, binding) {
        hasPermission(binding.value, el)
    },
}

export default permission
